<script setup lang="ts">
import APIDetails from '@/api/details'
import { useAuth } from '@/stores/index'
import type { Props, Emits } from './utils/types'
import UploadDataFile from './components/UploadDataFile/index.vue'
import { ElMessage, ElLoading } from 'element-plus';

const dialogVisible = shallowRef(false)
const $props = defineProps<Props>()
const $emit = defineEmits<Emits>()
const { info } = storeToRefs(useAuth())

watch($props, (newValue) => {
    dialogVisible.value = newValue.modelValue
}, { immediate: true })

watch(dialogVisible, (newValue) => {
    if (newValue) {
        getReportsType()
    }
    $emit('update:modelValue', newValue)
})

function onBeforeClose() {
    formData.value = {
        reports_files: [undefined, undefined, undefined, undefined],
        auth_files: [undefined, undefined, undefined, undefined]
    }
    checked.value = undefined
    dialogVisible.value = false
}

const reportsTypeList = ref<any[]>([])

function getReportsType() {
    APIDetails.getReportsType().then(({ data }) => {
        reportsTypeList.value = data
    })
}

const formData = ref<any>({
    reports_files: [undefined, undefined, undefined, undefined],
    auth_files: [undefined, undefined, undefined, undefined]
})

function selectWhistleblower() {
    window.location.href = `${import.meta.env.VITE_APP_CONSOLE_URL}`
}

const checked = ref<any>()

function submitFormData() {
    if (!formData.value.type) return ElMessage.error('请选择举报类型')
    if (formData.value.type == 'tort') {
        for (let item of formData.value.reports_files) if (!item) return ElMessage.error('请上传4张侵权截图')
        for (let item of formData.value.auth_files) if (!item) return ElMessage.error('请上传4张自我证明材料')
    }
    if (formData.value.type == 'resource_repeat' && !formData.value.goods_code_refer) return ElMessage.error('请输入重复商品编码')
    if (!formData.value.describe) return ElMessage.error('请输入诉求')
    if (!checked.value) return ElMessage.error('请勾选知晓并签署')
    formData.value.goods_id = $props.goodsInfo.id
    const form = { ...formData.value }
    if (formData.value.type == 'tort') {
        form.reports_files = form.reports_files.join(',')
        form.auth_files = form.auth_files.join(',')
    } else {
        delete form.reports_files
        delete form.auth_files
    }

    form.goods_type = $props.goodsInfo.type
    form.goods_code = $props.goodsInfo.code
    const loading = ElLoading.service({ lock: true })

    APIDetails.setReports(form).then(() => {
        ElMessage.success('举报成功')
        onBeforeClose()
    }).finally(() => [
        loading.close()
    ])
}

const $router = useRouter()

function handleJumpAgreement(type: string, mark: string) {
    window.open($router.resolve(`/agreement?type=${type}&mark=${mark}`).href, '_blank')
}

</script>

<template>
    <el-dialog class="dialog-report" v-model="dialogVisible" width="1124" destroy-on-close :before-close="onBeforeClose"
        :show-close="false">
        <template #header>
            <DialogClose title="举报作品" @close="onBeforeClose" />
        </template>
        <div class="dialog-report-content">
            <div class="left">
                <video v-if="goodsInfo?.type == 'video'" controls :src="goodsInfo?.sample"
                    :poster="goodsInfo?.image"></video>
                <img class="picture" v-if="goodsInfo?.type == 'picture'" :src="goodsInfo?.image" />
                <img class="picture" v-if="goodsInfo?.type == 'music'" :src="goodsInfo?.image" />
                <img class="picture" v-if="goodsInfo?.type == 'font'" :src="goodsInfo?.image" />
            </div>
            <div class="form">
                <div class="form-item">
                    <div class="title">基本信息：</div>
                    <ul class="form-info">
                        <li>
                            <div>
                                <span>类</span>
                                <span>型：</span>
                            </div>
                            <div>{{ goodsInfo.type_name }}</div>
                        </li>
                        <li>
                            <div>
                                <span>编</span>
                                <span>号：</span>
                            </div>
                            <div>{{ goodsInfo?.code }}</div>
                        </li>
                        <li>
                            <div>
                                <span>上</span>
                                <span>传</span>
                                <span>时</span>
                                <span>间：</span>
                            </div>
                            <div>{{ goodsInfo?.create_time }}</div>
                        </li>
                        <li>
                            <div>
                                <span>格</span>
                                <span>式：</span>
                            </div>
                            <div>{{ goodsInfo?.format }}</div>
                        </li>
                        <li>
                            <div>
                                <span>分</span>
                                <span>辨</span>
                                <span>率：</span>
                            </div>
                            <div>{{ goodsInfo?.width }}*{{ goodsInfo?.height }}</div>
                        </li>
                        <li>
                            <div>
                                <span>大</span>
                                <span>小：</span>
                            </div>
                            <div>{{ goodsInfo?.file_size }}</div>
                        </li>
                    </ul>
                </div>
                <div class="form-item">
                    <div class="title">举报类型：</div>
                    <el-radio-group v-model="formData.type">
                        <el-radio v-for="(item, index) in reportsTypeList" :key="index" :value="item.type">
                            {{ item.title }}
                        </el-radio>
                    </el-radio-group>
                </div>
                <div class="form-item" v-if="formData.type == 'resource_repeat'">
                    <div class="title">重复商品编码：</div>
                    <el-input v-model="formData.goods_code_refer" style="margin-top: 10px;"
                        placeholder="请输入重复商品编码"></el-input>
                </div>
                <div class="form-item">
                    <div class="title">举报人信息：</div>
                    <div v-if="info.is_auth == 0" class="select-whistleblower" @click="selectWhistleblower">选择举报人信息
                    </div>
                    <div class="whistleblower-info" v-else-if="info.is_auth == 1">
                        <div class="whistleblower-info-item">
                            <div class="label">姓名：</div>
                            <div class="value">{{ info?.auth_info?.real_name }}</div>
                        </div>
                        <div class="whistleblower-info-item">
                            <div class="label">身份证号码：</div>
                            <div class="value">{{ info.auth_info?.id_card }}</div>
                        </div>
                        <div class="whistleblower-info-item">
                            <div class="label">电话：</div>
                            <div class="value">{{ info?.phone }}</div>
                        </div>
                    </div>
                </div>
                <div class="form-item" v-if="formData.type == 'tort'">
                    <div class="title">侵权截图：</div>
                    <div class="files">
                        <UploadDataFile v-model="formData.reports_files[0]" title="上传资料" />
                        <UploadDataFile v-model="formData.reports_files[1]" title="上传资料" />
                        <UploadDataFile v-model="formData.reports_files[2]" title="上传资料" />
                        <UploadDataFile v-model="formData.reports_files[3]" title="上传资料" />
                    </div>
                    <div class="hint">请上传4张小于5MB的JPG格式的侵权材料。</div>
                </div>
                <div class="form-item" v-if="formData.type == 'tort'">
                    <div class="title">自我证明材料：</div>
                    <div class="files">
                        <UploadDataFile v-model="formData.auth_files[0]" title="上传资料" />
                        <UploadDataFile v-model="formData.auth_files[1]" title="上传资料" />
                        <UploadDataFile v-model="formData.auth_files[2]" title="上传资料" />
                        <UploadDataFile v-model="formData.auth_files[3]" title="上传资料" />
                    </div>
                    <div class="hint">请上传4张小于5MB的JPG格式的侵权材料。</div>
                </div>
                <div class="form-item">
                    <div class="title">诉求：</div>
                    <el-input :rows="5" style="margin-top: 10px; font-family: 'SOURCEHANSANSCN-EXTRALIGHT';"
                        type="textarea" v-model="formData.describe" placeholder="希望被举报人怎么样做，越详细越好。" />
                </div>
                <div class="form-item">
                    <div class="checkbox">
                        <el-checkbox v-model="checked" />
                        <div class="consent">知晓并签署<span @click="handleJumpAgreement('tort', 'tort')">《侵权投诉举报协议》</span>
                        </div>
                    </div>
                </div>
                <button type="button" @click="submitFormData" class="submit-form-data">提交举报</button>
            </div>
        </div>
    </el-dialog>
</template>

<style lang="scss">
.dialog-report {
    .el-radio {
        .el-radio__label {
            font-family: 'SOURCEHANSANSCN-EXTRALIGHT' !important;
        }
    }
}
</style>

<style lang="scss" scoped>
.dialog-report {
    padding-bottom: 20px;

    .dialog-report-content {
        display: flex;
        gap: 0 50px;

        .form {
            width: 550px;

            .submit-form-data {
                width: 231px;
                height: 40px;
                border-radius: 5px;
                background-color: rgba(249, 20, 20, 1);
                color: rgba(255, 255, 255, 1);
                font-size: 14px;
                border: none;
                cursor: pointer;
            }

            .form-item {
                margin-bottom: 30px;

                .checkbox {
                    display: flex;
                    align-items: center;

                    .consent {
                        font-size: 14px;
                        margin-left: 10px;
                        line-height: 1em;
                        font-family: 'SOURCEHANSANSCN-EXTRALIGHT';

                        span {
                            color: rgba(55, 133, 242, 1);
                            cursor: pointer;
                        }
                    }
                }

                .hint {
                    color: rgba(206, 206, 206, 1);
                    font-size: 12px;
                    margin-top: 10px;
                }

                .files {
                    margin-top: 10px;
                    display: flex;
                    gap: 0 20px;
                }

                .select-whistleblower {
                    color: rgba(55, 133, 242, 1);
                    font-size: 14px;
                    cursor: pointer;
                    margin-top: 10px;
                }

                .whistleblower-info {
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    margin-top: 10px;

                    .whistleblower-info-item {
                        display: flex;
                        align-items: center;

                        .label {
                            color: rgba(16, 16, 16, 1);
                            font-size: 14px;
                            font-family: 'SOURCEHANSANSCN-EXTRALIGHT';
                        }

                        .value {
                            border-radius: 5px;
                            color: rgba(136, 136, 136, 1);
                            font-size: 14px;
                            border: 1px solid rgba(187, 187, 187, 1);
                            padding-left: 10px;
                            padding-right: 20px;
                            line-height: 25px;
                        }
                    }
                }

                .form-info {
                    list-style: none;
                    width: 95%;
                    margin: 0 auto;
                    margin-top: 5px;

                    li {
                        display: flex;
                        margin-bottom: 8px;

                        .operation-btns {
                            margin-left: auto;

                            button {
                                width: 42px;
                                height: 18px;
                                line-height: 18px;
                                border-radius: 5px;
                                background-color: transparent;
                                color: rgb(206, 206, 206);
                                font-size: 13px;
                                border: 1px solid rgb(206, 206, 206);
                                margin-right: 10px;
                                cursor: pointer;
                            }
                        }

                        .blue-color {
                            color: #3785F2;
                        }

                        div {
                            color: rgba(154, 154, 154, 1);
                            font-size: 14px;

                            &:nth-child(1) {
                                width: 5em;
                                display: flex;
                                align-items: center;
                                justify-content: space-between;
                                font-family: 'SOURCEHANSANSCN-LIGHT';
                            }

                            &:nth-child(2) {
                                margin-left: 5px;
                                font-family: 'SOURCEHANSANSCN-EXTRALIGHT';
                            }
                        }
                    }
                }

                .title {
                    color: rgba(16, 16, 16, 1);
                    font-size: 14px;
                    display: flex;
                    align-items: center;
                    font-family: 'SOURCEHANSANSCN-REGULAR';

                    &::before {
                        content: '';
                        display: block;
                        width: 3px;
                        height: 15px;
                        background-color: #FE0000;
                        margin-right: 5px;
                    }
                }
            }

        }

        .left {
            width: 256px;
            height: 256px;
            background-color: #ECF5FE;
            display: flex;
            align-items: center;
            justify-content: center;

            .picture {
                width: 100%;
                height: 100%;
                object-fit: contain;
            }

            video {
                width: 100%;
            }
        }
    }
}
</style>

<style lang="scss">
.dialog-report .el-checkbox__input.is-checked .el-checkbox__inner {
    background-color: red;
    border-color: red;
}

.dialog-report .el-checkbox__inner:hover {
    border-color: red;
}
</style>